package com.tadpole.music.utils;


import com.tadpole.music.bean.question.DrumBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created By 孙岩
 * on 2023/9/1
 */
public class GuUtils {
    //用于记录敲击对错的返回集合
    private List<DrumBean> resultList = new ArrayList<>();
    //解析服务器后的集合
    private List<String> drumList;
    // 用于记录该计算那个答案的进度
    private int progress = 0;
    //音乐开始播放时候的时间戳
    private long thisTime;


    //初始化
    public  void initDrum(String drum) {
        drumList = Arrays.asList(drum.split(",\\|,"));
        for (int i = 0; i < drumList.size(); i++) {
            List<String> a = Arrays.asList(drumList.get(i).split(","));
            DrumBean drumBean  = new DrumBean(
                    0,a.get(0),a.get(1)
            );
            resultList.add(drumBean);
        }
        thisTime = Mutils.getTimeStame();
    }


    /**
     * 敲击
     *
     * @param drumTime 当前敲击的时间
     */
    public void drum(long drumTime) {
        long time = drumTime - thisTime;

        //开始以前
        if (time < Long.parseLong(resultList.get(0).getStart_time())){
            return;
        }
        //结束以后
        if (time >= Long.parseLong(resultList.get(resultList.size()-1).getEnd_time())) {

            return;
        }
        for (int i = progress; i < resultList.size(); i++) {
            if (time <= Long.parseLong(resultList.get(i).getEnd_time())) {
                progress = i;
                break;
            }
        }

        if (progress + 1 > resultList.size()) {
            return;
        } else {
            if (time < Long.parseLong(resultList.get(progress).getStart_time())) {
                resultList.get(progress-1).setResult(0);
            } else if (time >= Long.parseLong(resultList.get(progress).getStart_time()) && time <= Long.parseLong(resultList.get(progress).getEnd_time())) {
                // 设置当前答案为正确
                resultList.get(progress).setResult(1);
                // self.postMessage({code: 202,value:"设置当前答案为正确"})
                progress++;
            }
        }
    }

    //获取结果
    public List<DrumBean> getResultList(){
        return  resultList;
    }
}
